//
//  SortViewController.m
//  项目PianKe
//
//  Created by 关雷 on 14-12-27.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "SortViewController.h"
#import "ReadSortCollectionViewCell.h"
#import "AFNetWoringYu.h"
#import "UIImageView+WebCache.h"
#import "SortModel.h"
#import "UIScrollView+MJRefresh.h"
#import "WebColor.h"
#import "SortDetailViewController.h"
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
@interface SortViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@end

@implementation SortViewController

- (void)dealloc
{
    [_collectionArray release];
    [_collectionV release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.view.frame = CGRectMake(0, 45, self.view.frame.size.width, self.view.frame.size.height - 45);
        self.collectionArray = [NSMutableArray array];
        self.view.backgroundColor = [WebColor gainsboro];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createCollecttionView];
    [self startConnect];
    [self headerRefreshing];
    
    // Do any additional setup after loading the view.
}
//网络请求,数据解析
- (void)startConnect
{
    [AFNetWoringYu netWorkWithURL:@"http://api.pianke.me/read/category" body:@"client=1&latestpic=1" resultBlock:^(id dic) {
        NSDictionary * dics = [dic objectForKey:@"data"];
        NSArray * list = [dics objectForKey:@"list"];
        for (NSDictionary * dica in list) {
            SortModel * model = [[SortModel alloc] init];
            [model setValuesForKeysWithDictionary:dica];
            [_collectionArray addObject:model];
            [model release];
        }
        [_collectionV reloadData];
    }];
}
//创建collecttionView
- (void)createCollecttionView
{
    UICollectionViewFlowLayout * flow = [[UICollectionViewFlowLayout alloc] init];
    flow.minimumLineSpacing = 10;
    flow.minimumInteritemSpacing = 15;
    flow.sectionInset = UIEdgeInsetsMake(10, 5, 170, 5);
    flow.itemSize = CGSizeMake(SCREEN_WIDTH / 2 - 20, SCREEN_WIDTH / 2);
    self.collectionV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flow];
    _collectionV.dataSource = self;
    _collectionV.delegate = self;
    _collectionV.backgroundColor = [UIColor whiteColor];
    [_collectionV registerClass:[ReadSortCollectionViewCell class] forCellWithReuseIdentifier:@"cellSort"];
    [self.view addSubview:_collectionV];
    [_collectionV release];
    [flow release];
}
//cell的数量
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [_collectionArray count];
}
//为cell赋值
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    ReadSortCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellSort" forIndexPath:indexPath];
    NSArray * array = [NSArray arrayWithObjects:@"readSort1.png", @"readSort2.png", @"readSort3.png", @"readSort4.png", @"readSort5.png", @"readSort6.png", nil];
    SortModel * model = [_collectionArray objectAtIndex:indexPath.row];
    [cell.imgV setImageWithURL:[NSURL URLWithString:model.coverimg]];
    cell.myImgV.image = [UIImage imageNamed:[array objectAtIndex:indexPath.row]];
    cell.lable.text = model.desc;
  
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [collectionView deselectItemAtIndexPath:indexPath animated:NO];
    SortModel * sModel = [_collectionArray objectAtIndex:indexPath.row];
    SortDetailViewController * sortD = [[SortDetailViewController alloc] init];
    NSInteger types = [sModel.type integerValue];
    sortD.type = types;
    [self.navigationController pushViewController:sortD animated:YES];
    [sortD release];
}
//下拉刷新
- (void)headerRefreshing
{
    [_collectionV addHeaderWithCallback:^{
        [AFNetWoringYu netWorkWithURL:@"http://api.pianke.me/read/category" body:@"client=1&latestpic=1" resultBlock:^(id dic) {
            [_collectionArray removeAllObjects];
            NSDictionary * dics = [dic objectForKey:@"data"];
            NSArray * list = [dics objectForKey:@"list"];
            for (NSDictionary * dica in list) {
                SortModel * model = [[SortModel alloc] init];
                [model setValuesForKeysWithDictionary:dica];
                [_collectionArray addObject:model];
                [model release];
            }
            [_collectionV reloadData];
            [_collectionV headerEndRefreshing];
        }];
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
